MatFa's Boids 0.1, by matfa@acacia.se
I wrote MatFa's Boids 0.1 during February 1997, 'cause it seemed the thing to do. It took all of two days to get running, but I knew the rules I needed in advance. My lacking memory of trigonometry caused a lot of those days to be spent hunting dumb bugs. A couple of more days were spent on making it look nicer, and writing this document, so I could release it into the public domain.

This program was written entirely in Delphi 2.0, a wonderful programming environment from Borland. If you find the program a tad slow, it is *not* due to Delphi, but rather that I didn’t optimize it as much as I could have.

The Boids of Craig Reynolds
Who better too explain the boids than their inventor, Craig Reynolds? I 'borrowed' this passage from his webpage, at http://hmt.com/cwr/boids.html, the premiere source on boids on the 'net. There are other implementations of the boids rules there. 

"In 1986 I made a computer model of coordinated animal motion such as bird flocks and fish schools. It was based on three dimensional computational geometry of the sort normally used in computer animation or computer aided design. I called the software boids. 

Each boid has direct access to the whole scene's geometric description, but reacts only to flockmates within a certain small radius of itself. The basic flocking model consists of three simple steering behaviors: 

1.Separation: steer to avoid crowding local flockmates. 
2.Alignment: steer towards the average heading of local flockmates. 
3.Cohesion: steer to move toward the average position of local flockmates. 

In addition, the more elaborate behavioral model included predictive obstacle avoidance and goal seeking. Obstacle avoidance allowed the boids to fly through simulated environments while dodging static objects. For applications in computer animation, a low priority goal seeking behavior caused the flock to follow a scripted path."

The Boids of Mattias Fagerlund
Well, my boids are exactly like Craig's, though I have written all the code myself. There are some visual cues as to what the Boids are doing. 

•	Green boids are turning because of the obstacles
•	Red boids are breaking
•	Blue boids are accelerating
•	White boids are keeping constant pace

The boids themselves
Every boid in the flock is in turn asked to decide what he wants to do; break, accelerate, turn left or turn right. He is given a list of the boids that are within his sensor range, which is set to 50 pixels by default at the moment. He is also given the nearest obstacle, if there are any obstacles in the arena.

1. Separation: steer to avoid crowding local flockmates. 
My boids aren't very clever, they only keep track of one neighbor when it comes to separation. So if avoiding one means hitting another, the boids won't hesitate; they know of no other boids than the closest one.

If this rule is fired, that is, one Boid is within the closest allowed distance, the boid steers away from the boid in question. Since separation steering should take higher priority than steering that other rules might order, future attempts to steer are limited.

The boid also slams the breaks if the boid he's getting to close too is ahead, and he accelerates if the boid in question is behind him.

2. Alignment: steer towards the average heading of local flockmates.
An average heading is calculated amongst the boids in range, and then the boid adjusts his own steering thereafter.

3. Cohesion: steer to move toward the average position of local flockmates.
This rule is executed in much the same way as the former rule.

The circles / obstacles, or Rule zero
My boids tend to get a bit dull if there's nothing in the system to prevent them from all converging into one big flock. So I inserted the 'obstacle' which is a yellow blob the Boids try to avoid. I call that rule zero; "don't hit the blob". That livens up the lives of the boids.

They don't always avoid the obstacle, I'm sorry to say, partly because it's a tradeoff between avoiding the other boids, and partly because they only perceive *one* obstacle at a time; they don't plan ahead. If two obstacles are in range, they avoid the closer one, even if it means going head on into another obstacle.

Possible enhancements / future uses
There are many ways in which this code could be enhanced; the boids could take more than one neighbor into account when trying to avoid crashes. They could take more than one obstacle into account too. Other kinds of obstacles could be added, lines for instance, so that perimeter of the window could function as walls. I will probably not make these enhancements though.

The code could easily be used to output movement patterns that could later be rendered in 3D Studio or other 3D software packages. I will probably not this either, but if you would like to try it, I'd be glad to be of assistance.

A future enhancement would be to use WinG or DirectX to speed up the drawing, but for the time being, I won't.

I suspect the program would make a cool screensaver, but I won’t do that either ;), perhaps one day…

Copyright
There is no copyright on this program, you may use it as you see fit. Standard disclaimer applies; I don't take responsibility for damage that might occur due to the program! The source code, if it is not included in this package, can  be obtained by asking me at matfa@acacia.se .

However, even though there is no Copyright on this software, I would very much like to know if you like it. If you find it interesting, drop me a note! If you *really* like it, send me a postcard… ;) If you use it for something interesting, or improve it in some way, I'd love to know. 

Contacting the author
If you want to contact me I can be reached at matfa@acacia.se or by snailmail at;

Mattias Fagerlund
Skanörvägen 5 n.b.
121 52 Johanneshov
SWEDEN